package cn.pconline.autoclub.web;

import cn.pconline.autoclub.entity.AppBanner;
import cn.pconline.autoclub.service.AppBannerService;
import cn.pconline.autoclub.service.PagerService;
import cn.pconline.autoclub.util.AppUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author zhouruxuan
 * @date 2014年10月16日 上午9:42:05
 */

@Controller
public class AppBannerController {
	
	@Autowired
	private AppBannerService appBannerService;
	@Autowired
	private PagerService pagerService;

	private static final org.slf4j.Logger log = LoggerFactory
			.getLogger(GiftController.class);

	/**
	 * 返回相关Json信息
	 * @param resp
	 * @param str
	 * @param callback
	 */
	private void printMsg(HttpServletResponse resp, String str, String callback) {
		try {
			java.io.PrintWriter w = resp.getWriter();
			if (StringUtils.isNotBlank(callback)) {
				w.print(callback + "(" + str + ")");
			} else {
				w.print(str);
			}
			w.flush();
			w.close();
		} catch (IOException ex) {
			log.error("response输出出错了", ex);
		}
	}

	/**
     * 可选车友会顶部背景图列表
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/club/listAppBanner.do", method = RequestMethod.GET)
    public void listAppBanner4App(HttpServletRequest request, HttpServletResponse response) {
    	WebUtils.setEncoding(request, response);
        //获取相关参数
        String callback = WebUtils.param(request, "callback", "");
        int pageSize = WebUtils.paramInt(request, "pageSize", 20);
        int pageNo = WebUtils.paramInt(request, "pageNo", 1);
        JSONArray jsonArray = new JSONArray();
        JSONObject json = new JSONObject();
		//list转换可分页显示
        List<AppBanner> list = appBannerService.getAllList();
        Pager<AppBanner> pager = new Pager<AppBanner>(list);
        pager.setPageNo(pageNo);
        pager.setPageSize(pageSize);
        pager.setTotal(list.size());
        //判断pageNo是否正确
        if (pageNo > pager.getPageCount() || (pageNo - 1) * pageSize < 0) {
        	json.put("code", AppUtils.STATUS_DATA_ERROR);
        	json.put("message", "非法参数 pageNo!");
        	printMsg(response, json.toString(), callback);
            return ;
        }
        //设置返回信息到Json
        if(pager != null) {
        	for(AppBanner appBanner : pager.getResultList()) {
        		JSONObject jsonObject = new JSONObject();
            	jsonObject.put("id", appBanner.getBannerId());//背景ID
            	jsonObject.put("title", appBanner.getTitle());//背景名字
            	jsonObject.put("img", appBanner.getUrl());//背景图,APP3.1
            	jsonObject.put("img960x1200", appBanner.getUrl960x1200());//背景图,APP4.0
            	jsonObject.put("thumbImg", appBanner.getThumbImg());//缩略图,APP3.1
            	jsonObject.put("thumbImg640x800", appBanner.getThumbImg640x800());//缩略图,APP4.0
            	jsonArray.add(jsonObject);
        	}
        }
        
        json.put("data", jsonArray);
        json.put("total", pager.getTotal());
        json.put("pageNo", pager.getPageNo());
        json.put("pageSize", pager.getPageSize());
        json.put("code", AppUtils.STATUS_OK);
        json.put("message", "成功获取可选车友会顶部背景图列表！");
        printMsg(response, json.toString(), callback);
    }
}
